#Binary parser template

import os, sys
from Sanae3D.lib.StructReader import StructReader
from Sanae3D.lib.ModelObject import Model3D

class RaiderZ_elu(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(RaiderZ_elu,self).__init__("ELU")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        self.normals = []
        self.uvs = []
        
    def parse_faces(self, meshName):
        
        numFaces = self.inFile.read_long()
        print "%d faces" %numFaces
        self.inFile.seek(8, 1)
        for i in xrange(numFaces):
            vertCount = self.inFile.read_long()
            info = self.inFile.read_short(18)
            v1, v2, v3 = info[0], info[6], info[12]
            self.inFile.seek(2, 1) #unk
            
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
            
    def parse_vertices(self, meshName):
        
        numVerts = self.inFile.read_long()
        for i in xrange(numVerts):
            vx, vz, vy = self.inFile.read_float(3)
            
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
        
        numNorm = self.inFile.read_long()
        for i in xrange(numNorm):
            nx, ny, nz = self.inFile.read_float(3)
            self.normals.append([nx, ny, nz])
        
        unk3 = self.inFile.read_long()
        for i in xrange(unk3):
            self.inFile.read_float(4)
        
        unk4 = self.inFile.read_long()
        for i in xrange(unk4):
            self.inFile.read_float(3)
            
        numUV = self.inFile.read_long()
        for i in xrange(numUV):
            self.inFile.read_float(3)
                    
        unk5 = self.inFile.read_long()
        self.inFile.seek(unk5 * 12, 1)
        
    def parse_mesh(self, numMesh):
        
        charLen = self.inFile.read_long()
        meshName = self.inFile.read_string(charLen).strip("\x00")
        index = self.inFile.read_long()
        
        self.create_object(meshName)
        print meshName
        
        self.inFile.seek(5, 1)
        unk1 = self.inFile.read_long()
        if unk1 in [0x0, 0x2000]:
            self.inFile.seek(72, 1)
        else:
            print "unknown unk1", unk1
        
        self.parse_vertices(meshName)
        self.parse_faces(meshName)
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        magic = self.inFile.read_string(4)
        version, numMat, numMesh = self.inFile.read_long(3)
        self.parse_mesh(numMesh)
        
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = RaiderZ_elu(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "\x60\xF0\x07\x01", "ELU", "RaiderZ ELU"

if __name__ == '__main__':
    
    file1 = "frog.elu"
    file2 = "npc_drum01.elu"
    file3 = "temple_of_renas_arch_a_01.elu"
    file4 = "npc-obj_anvil01.elu"
    obj = read_file(file2)